package com.Tread.pool;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LexicalAnalyzer {

    public static void lexicalAnalysis(String inputStr) {
        // 定义正则表达式模式
        Pattern constPattern = Pattern.compile("\\bconst\\b");
        Pattern identifierPattern = Pattern.compile("[a-zA-Z_][a-zA-Z0-9_]*");
        Pattern charPattern = Pattern.compile("\'[^']*\'");
        Pattern stringPattern = Pattern.compile("\"[^\"]*\"");
        Pattern integerPattern = Pattern.compile("[+-]?\\d+");
        Pattern floatPattern = Pattern.compile("[+-]?\\d+\\.\\d+");

        // 初始化统计变量
        int charCount = 0;
        int stringCount = 0;
        int integerCount = 0;
        int floatCount = 0;

        // 判断是否以 "const" 开头
        Matcher constMatcher = constPattern.matcher(inputStr);
        if (constMatcher.find()) {
            // 去除空格
            inputStr = inputStr.replace(" ", "");

            // 分割输入字符串
            String[] declarations = inputStr.substring(5).split(",");

            for (String declaration : declarations) {
                // 判断常量名
                Matcher identifierMatcher = identifierPattern.matcher(declaration);
                if (!identifierMatcher.find()) {
                    System.out.println("Error: 无效符号");
                    return;
                }

                String constantName = identifierMatcher.group();
                declaration = declaration.substring(constantName.length());

                // 判断等号
                if (!declaration.startsWith("=")) {
                    System.out.println("Error: 没‘=’");
                    return;
                }

                declaration = declaration.substring(1);

                // 识别常量类型和值
                if (charPattern.matcher(declaration).matches()) {
                    charCount++;
                    String constantType = "char";
                    String constantValue = declaration.substring(1, declaration.length() - 1);
                    System.out.println("(" + constantName + ", " + constantType + ", " + constantValue + ")");
                } else if (stringPattern.matcher(declaration).matches()) {
                    stringCount++;
                    String constantType = "string";
                    String constantValue = declaration.substring(1, declaration.length() - 1);
                    System.out.println("(" + constantName + ", " + constantType + ", " + constantValue + ")");
                } else if (declaration.contains(".") && floatPattern.matcher(declaration).matches()) {
                    floatCount++;
                    String constantType = "float";
                    String constantValue = declaration;
                    System.out.println("(" + constantName + ", " + constantType + ", " + constantValue + ")");
                } else if (integerPattern.matcher(declaration).matches()) {
                    integerCount++;
                    String constantType = "integer";
                    String constantValue = declaration;
                    System.out.println("(" + constantName + ", " + constantType + ", " + constantValue + ")");
                } else {
                    System.out.println("Error: 无效值");
                    return;
                }
            }

            // 输出统计信息
            System.out.println("Integer Count: " + integerCount);
            System.out.println("Float Count: " + floatCount);
            System.out.println("Char Count: " + charCount);
            System.out.println("String Count: " + stringCount);
        } else {
            System.out.println("Error: 必须以'const'开头.");
        }
    }

    public static void main(String[] args) {
        java.util.Scanner scanner = new java.util.Scanner(System.in);
        while (true) {
            System.out.print("请输入字符串: ");
            String inputStr = scanner.nextLine();
            if (inputStr.toLowerCase().equals("exit")) {
                break;
            }
            lexicalAnalysis(inputStr);
        }
    }
}
